﻿using System;
using System.Collections.Generic;
using System.Drawing;
using Timer = System.Windows.Forms.Timer;
using System.Media;
using System.Windows.Forms;
using System.Threading;
/* Authors: Mike Poddo, Matt Gillette
 * Missile Command
 */


namespace MissileCommand
{
    class OffensiveMissileShots
    {
        private Cities cities;
        private Graphics g;
        private CitiesBoom boom;
        private List<PointF> PointList = new List<PointF>();
        private Boolean active;
        private Timer OffensiveMissileTimer;
        private int pointIndex = 0;
        private int randomStartPT;
        private int randomStopPT;
        private int pointsPerHit = 0;
        private RectangleF r;
        private SolidBrush mybrush;
        //setup start/stop points
        private PointF[] PossibleStartPoints = new PointF[] { new PointF(34, 28), new PointF(84, 28), new PointF(157, 28), new PointF(217, 28), new PointF(283, 28), new PointF(340, 28) };
        //private PointF[] PossibleEndPoints = new PointF[] { new PointF(60, 260), new PointF(193, 260), new PointF(345, 260), new PointF(94, 272) }; //needs to be the same as Cities Points
        private PointF[] PossibleEndPoints = new PointF[] { new PointF(94, 308), new PointF(126, 308), new PointF(155, 308), new PointF(235, 308), new PointF(268, 308), new PointF(306, 308) };
        

        public OffensiveMissileShots(Graphics g, ref Cities cities, int endPt, int pointsPerHit)
        {
            this.pointsPerHit = pointsPerHit;
            randomStartPT = RandomStartPoint();
            randomStopPT = endPt;
            this.g = g;
            boom = new CitiesBoom(g, PossibleEndPoints[randomStopPT]);
            mybrush = new SolidBrush(Color.Transparent);
            CalcPoints();
            this.cities = cities;
            OffensiveMissileTimer = new Timer();
            OffensiveMissileTimer.Interval = 150;
            OffensiveMissileTimer.Tick += new EventHandler(Draw);
            OffensiveMissileTimer.Start();
            active = true;
        }

        public void FireShot()
        {
            CalcPoints();
            //this.cities = cities;
            OffensiveMissileTimer = new Timer();
            OffensiveMissileTimer.Interval = 150;
            OffensiveMissileTimer.Tick += new EventHandler(Draw);
            OffensiveMissileTimer.Start();
            active = true;
        }

        private void Draw(object sender, System.EventArgs e)
        {
            try
            {
                if (this.pointIndex < PointList.Count & this.active)
                {
                        g.DrawLine(Pens.AliceBlue, PossibleStartPoints[randomStartPT].X,PossibleStartPoints[randomStartPT].Y, PointList[pointIndex].X,PointList[pointIndex].Y);
                        r = new RectangleF(PointList[pointIndex].X, PointList[pointIndex].Y, 1, 1);
                        //draw a rectangle on the end of the line
                        g.FillRectangle(mybrush, r);
                    pointIndex++;
                }
                else //hit a city
                {
                    this.active = false;
                    OffensiveMissileTimer.Stop();
                    Pen myPen = new Pen(Color.Black);
                    myPen.Width = 5;
                    g.DrawLine(myPen, PossibleStartPoints[randomStartPT].X, PossibleStartPoints[randomStartPT].Y, PointList[pointIndex - 1].X, PointList[pointIndex - 1].Y);
                    //draw boom
                    boom.drawBoom();
                    cities.CityRemove(randomStopPT);
                    MissileCommand.ModifyShotList(randomStopPT);
                }
            }
            catch
            {}
        }

        public RectangleF CurrentRectangle()
        {
            return r;
        }

        public void OffensiveMissileHit()
        {
            //hit a missile
            MissileCommand.addMissileHit = 1;
            MissileCommand.ModifyPlayerScore(pointsPerHit);
            OffensiveMissileTimer.Stop();
            Thread.Sleep(10);
            MissileCommand.ModifyShotList(randomStopPT);
            Pen myPen = new Pen(Color.Black);
            myPen.Width = 5;
            try
            {
                g.DrawLine(myPen, PossibleStartPoints[randomStartPT].X, PossibleStartPoints[randomStartPT].Y, PointList[pointIndex - 1].X, PointList[pointIndex - 1].Y);
            }
            catch (Exception)
            {

            }
        }

        private void CalcPoints()
        {
            // Get Points From Line(s)
            float distance = 0;
            float curDist = 0;
            PointF ptA = PossibleStartPoints[randomStartPT];
            PointF ptB = PossibleEndPoints[randomStopPT];
            float deltaX = ptB.X - ptA.X;
            float deltaY = ptB.Y - ptA.Y;
            distance = (float)Math.Sqrt(Math.Pow(deltaX, 2) + Math.Pow(deltaY, 2));

            while (curDist < distance)
            {
                curDist += 6; //adjust speed of line
                float offsetX = (float)((double)curDist / (double)distance * (double)deltaX);
                float offsetY = (float)((double)curDist / (double)distance * (double)deltaY);
                PointF point = new PointF(ptA.X + offsetX, ptA.Y + offsetY);
                //if(PointList.Contains(point))
                PointList.Add(point);
            }
        }
        
        private int RandomStartPoint()
        {
            Random random = new Random();
            return random.Next(PossibleStartPoints.Length); 
        }
        
        private int RandomEndPoint()
        {
            Random random = new Random();
            //MessageBox.Show("" + PossibleEndPoints.Length);
           return random.Next(PossibleEndPoints.Length);
        }

        public void Dispose()
        {
            g.Dispose();
            OffensiveMissileTimer.Stop();
        }

        public void PauseMe()
        {
            OffensiveMissileTimer.Stop();
        }

        public void UnPauseMe()
        {
            OffensiveMissileTimer.Start();
        }


    }
}
